home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 949 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.2 KB

  1. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  2. Message-ID: <31629bfb.9683167@news.ultranet.com>
  3. X-Original-Date: Wed, 03 Apr 1996 16:07:46 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 03 Apr 96 16:33:41 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Is upcasting a conversion? -- Conversion operator ambiguity
  9. Organization: UltraNet Communications, Inc.
  10. X-Newsreader: Forte Agent .99d/16.182
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMWKoa+EDnX0m9pzZAQH2TgF/dB1y+POVXN0XTpTz/UjN5+I5lfA8HASF
  13.     uZsehq4QydoywK8iEKCYepyBk4PjLZpK
  14.     =LigN
  15.  
  16. I want to describe a language issue that may seem perverse conceptually
  17. but which came up in reality when using templates. (I have noticed that
  18. templates bring up many of the degenerate cases in the language).  The
  19. problem is best illustrated with the following code:
  20.  
  21. class x {
  22. public:
  23.   x();
  24.   operator x() const;  // LINE A: conversion from x to x
  25. };
  26.  
  27. class y : public x
  28. {
  29. };
  30.  
  31. void main()
  32. {
  33.   y Y;
  34.   x X = Y;   // LINE B: Which conversion? X(X) or y.operator x() ?
  35. }
  36.  
  37. There are two distinct but related issues here. First, in class x, is it
  38. valid to define an operator that converts an x to an x as in LINE A,
  39. above? The situation comes up in the use of templates as follows:
  40.  
  41. template <class T>
  42. class Q { public: operator Q<const int>() const; }
  43.  
  44. If T is const int, then the conversion operator becomes a conversion of
  45. a class to itself. My compiler (Borland 4.02) doesn't mind this and the
  46. code works as intended (converting any Q<T> to a Q<const int>). I think
  47. this is good, so I hope it is, in fact legal according to the DWP.
  48.  
  49. It would seem that a conversion operator x::operator x() would never be
  50. used since there is never a need to convert from an object to itself.
  51. The problem comes in when inheritence and upcasting are involved, as in
  52. LINE B, above. In this case, my compiler complains of an ambiguity
  53. between "x::operator x() const" and "x::x(const x&)". It is interesting
  54. to note that if I change the declaration of X to "x X(Y)" the compiler
  55. does not complain, so there is definately a bug in the compiler. The
  56. question is, what is the bug? Should the compiler complain about an
  57. ambiguity or not?
  58.  
  59. My questions, therefore, are as follows:
  60.  
  61.   Is it valid to define an operator to convert x to x?
  62.   Is upcasting from a derived class to a base class considered a
  63.     conversion, equal in precidence to user-defined conversions?
  64.   Can user-defined conversions ever override built-in conversions (e.g. 
  65.     from x to const x, from derived to base, from x to x&, etc.)?
  66.   If there is a real ambiguity, is there another way I can get my 
  67.     template class to work without running a foul of it?
  68.  
  69. -------------------------------------------------------------
  70. Pablo Halpern                   phalpern@truffle.ultranet.com
  71.  
  72. I am self-employed. Therefore, my opinions *do* represent 
  73. those of my employer.
  74. ---
  75. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  76. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  77. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  78. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  79. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  80.